package com.showsu.editor.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.showsu.editor.dao.TblXhsDewaterRecordMapper;
import com.showsu.editor.entity.po.TblXhsDewaterRecord;
import com.showsu.editor.entity.vo.request.RequestVO;
import com.showsu.editor.entity.vo.response.XhsLiveVO;
import com.showsu.editor.enums.RowFlagEnum;
import com.showsu.editor.service.IXhsDewaterRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author: William
 * @Description:
 * @Date: Created in 2:10 下午 2024/9/14
 * @Modified By:
 */
@Service
@Slf4j
public class XhsDewaterRecordServiceImpl extends ServiceImpl<TblXhsDewaterRecordMapper, TblXhsDewaterRecord> implements IXhsDewaterRecordService {

    @Autowired
    private TblXhsDewaterRecordMapper tblXhsDewaterRecordMapper;

    @Override
    public int saveShowRecord(RequestVO clientRequest, Long userId,Integer type, String source, XhsLiveVO result) {
        TblXhsDewaterRecord tblXhsDewaterRecord = new TblXhsDewaterRecord();
        tblXhsDewaterRecord.setPlatform(1);
        tblXhsDewaterRecord.setType(type);
        tblXhsDewaterRecord.setClientBrowser(clientRequest.getClientBrowser());
        tblXhsDewaterRecord.setClientIp(clientRequest.getClientIp());
        tblXhsDewaterRecord.setClientOrigin(clientRequest.getClientOrigin());
        tblXhsDewaterRecord.setClientOs(clientRequest.getClientOs());
        tblXhsDewaterRecord.setResult(result == null ? "" : JSON.toJSONString(result));
        tblXhsDewaterRecord.setUserId(userId);
        tblXhsDewaterRecord.setSource(source);
        int rows = tblXhsDewaterRecordMapper.insert(tblXhsDewaterRecord);
        return rows;
    }

    @Override
    public int saveShowRecord(RequestVO clientRequest, Long userId, Integer type, String source, String result) {
        TblXhsDewaterRecord tblXhsDewaterRecord = new TblXhsDewaterRecord();
        tblXhsDewaterRecord.setPlatform(1);
        tblXhsDewaterRecord.setType(type);
        tblXhsDewaterRecord.setClientBrowser(clientRequest.getClientBrowser());
        tblXhsDewaterRecord.setClientIp(clientRequest.getClientIp());
        tblXhsDewaterRecord.setClientOrigin(clientRequest.getClientOrigin());
        tblXhsDewaterRecord.setClientOs(clientRequest.getClientOs());
        tblXhsDewaterRecord.setResult(result);
        tblXhsDewaterRecord.setUserId(userId);
        tblXhsDewaterRecord.setSource(source);
        int rows = tblXhsDewaterRecordMapper.insert(tblXhsDewaterRecord);
        return rows;
    }

    @Override
    public int queryCountOfUser(Long userId) {
        if (userId == null) {
            return 0;
        }
        return tblXhsDewaterRecordMapper.selectCount(new QueryWrapper<TblXhsDewaterRecord>().lambda()
                .eq(TblXhsDewaterRecord::getUserId, userId)
                .eq(TblXhsDewaterRecord::getFlag, RowFlagEnum.UnDelete.getCode())
                );
    }

    @Override
    public int queryCountOfUserWithDate(Long userId, Date startDate, Date endDate) {
        if (userId == null || startDate == null || endDate == null) {
            return 0;
        }
        return tblXhsDewaterRecordMapper.selectCount(new QueryWrapper<TblXhsDewaterRecord>().lambda()
                .eq(TblXhsDewaterRecord::getUserId, userId)
                .ge(TblXhsDewaterRecord::getCreateTime, startDate)
                .le(TblXhsDewaterRecord::getCreateTime, endDate)
                .eq(TblXhsDewaterRecord::getFlag, RowFlagEnum.UnDelete.getCode()));
    }

    @Override
    public int queryCountOfUser(Long userId,Integer type) {
        if (userId == null) {
            return 0;
        }
        return tblXhsDewaterRecordMapper.selectCount(new QueryWrapper<TblXhsDewaterRecord>().lambda()
                .eq(TblXhsDewaterRecord::getUserId, userId)
                .eq(TblXhsDewaterRecord::getType, type)
                .eq(TblXhsDewaterRecord::getFlag, RowFlagEnum.UnDelete.getCode())
                );
    }

    @Override
    public int queryCountOfUserWithDate(Long userId,Integer type, Date startDate, Date endDate) {
        if (userId == null || startDate == null || endDate == null) {
            return 0;
        }
        return tblXhsDewaterRecordMapper.selectCount(new QueryWrapper<TblXhsDewaterRecord>().lambda()
                .eq(TblXhsDewaterRecord::getUserId, userId)
                .eq(TblXhsDewaterRecord::getType, type)
                .ge(TblXhsDewaterRecord::getCreateTime, startDate)
                .le(TblXhsDewaterRecord::getCreateTime, endDate)
                .eq(TblXhsDewaterRecord::getFlag, RowFlagEnum.UnDelete.getCode()));
    }
}
